package com.spittr.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;

import com.annotation.DevelopProfile;
import com.annotation.QaProfile;

/**
 * 数据库连接配置
 * 实现事务
 * @author zoe
 * 2017年2月9日
 */
@Configuration
@PropertySource("classpath:db.properties")
public class DbConfig {
	
	@Autowired
	public Environment environment;
	
	/**
	 * 数据库连接
	 * jdbc驱动
	 * @return
	 */
	@Bean
	@DevelopProfile
	public DataSource developDataSource(){
		DriverManagerDataSource ds = new DriverManagerDataSource();
		String driverClassName = environment.getRequiredProperty("jdbc.driverClassName");
		String url = environment.getRequiredProperty("jdbc.url");
		String username = environment.getRequiredProperty("jdbc.username");
		String password = environment.getRequiredProperty("jdbc.password");
		ds.setDriverClassName(driverClassName);
		ds.setUrl(url);
		ds.setUsername(username);
		ds.setPassword(password);
		return ds;
	}
	
	@Bean
	@QaProfile
	public DataSource qaDataSource(){
		DriverManagerDataSource ds = new DriverManagerDataSource();
		String driverClassName = environment.getRequiredProperty("jdbc.driverClassName");
		String url = environment.getRequiredProperty("qa.jdbc.url");
		String username = environment.getRequiredProperty("qa.jdbc.username");
		String password = environment.getRequiredProperty("qa.jdbc.password");
		ds.setDriverClassName(driverClassName);
		ds.setUrl(url);
		ds.setUsername(username);
		ds.setPassword(password);
		return ds;
	}
	
	/**
	 * 装配sessionFactory
	 * @param dataSource
	 * @return
	 */
	@Bean
	public LocalSessionFactoryBean sessionFactory(DataSource dataSource){
		LocalSessionFactoryBean sfb = new LocalSessionFactoryBean();
		sfb.setDataSource(dataSource);
		sfb.setPackagesToScan("com.spittr.web.model");
		Properties properties = new Properties();
		properties.setProperty("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
		properties.setProperty("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
		properties.setProperty("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
		sfb.setHibernateProperties(properties);
		return sfb;
	}
	
	/**
	 * 装配hibernat事务管理器
	 * @param sessionFactory
	 * @return
	 */
	@Bean
	public HibernateTransactionManager hibernateTransactionManager(SessionFactory sessionFactory){
		HibernateTransactionManager htm = new HibernateTransactionManager(sessionFactory);
		return htm;
	}

}
